<?php

namespace app\index\controller;
use houdunwang\arr\Arr;
use think\Controller;
use \app\common\model\User as UserModel;
class User extends Common
{

    public function login()
    {
//        dump(session('name'));
//        dump(session('?name'));
//        $cartdata = $this->category();

        $cdata = db('category')->limit(5)->select();
        $field = Arr::channelLevel(db('category')->where('id>5')->select(), $pid = 0, $html = "&nbsp;", $fieldPri = 'id', $fieldPid = 'pid');

        return $this->template('login',compact('field','cdata',''));
    }

    public function register()
    {
//        dump(session('name'));
//        dump(session('?name'));
//        $cartdata = $this->category();
        $cdata = db('category')->limit(5)->select();
        $field = Arr::channelLevel(db('category')->where('id>5')->select(), $pid = 0, $html = "&nbsp;", $fieldPri = 'id', $fieldPid = 'pid');

        return $this->template('register',compact('field','cdata',''));
    }

    /**
     * 1.登陆成功
     * 2.用户名不能为空
     * 3.密码不能为空
     * 4.用户名不正确
     * 5.密码错误
     * @param LoginModel $login
     * @return array
     */
    public function loginajax(UserModel $user)
    {
        if (request()->isAjax())
        {
            $post = input('post.');
            if (!$post['user'])
            {
                return [['message'=>2] ];#用户名不能为空
            }
            if (!$post['pw'])
            {
                return [['message'=>3 ]];#密码不能为空
            }

            $res = $user->check($post);
            if ($res==1)
            {

                return 1;
            }
        }
    }
    /**
     * 2.用户名不能为空
     * 3.设置密码不能为空
     * 4.确认密码不能为空
     * 5.两次密码输入不一致
     * 1.登陆成功
     * @param User $user
     * @return array|mixed
     */
    public function registerajax(UserModel $user)
    {
        if (request()->isAjax())
        {
            $username = input('user');
            $password= input('pw');
            $password1= input('c_pw');
            if (!$username)
            {
                return [['message'=>2] ];#用户名不能为空
            }
            if (!$password)
            {
                return [['message'=>3 ]];#设置密码不能为空
            }
            if (!$password1)
            {
                return [['message'=>4] ];#确认密码不能为空
            }
            if ($password1!=$password)
            {
                return [['message'=>5] ];#密码不一致请重新输入
            }

            $res = $user->add($username,$password);
//            halt($res);

        }
        return 1;

    }

}